package org.jeecg.modules.wlhy.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.HttpTools;
import org.jeecg.modules.api.wlhy.app.bean.ResBaseBean;
import org.jeecg.modules.api.wlhy.app.utils.HttpUtils;
import org.jeecg.modules.third.ecloud.sdk.bean.EcloudPublicKey;
import org.jeecg.modules.wlhy.hybank.entity.HyBank;
import org.jeecg.modules.wlhy.hybank.service.IHyBankService;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgItemService;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyconfig.service.IHyConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Service
public class InitService {

    @Autowired
    private IHyConfigService hyConfigService;

    @Autowired
    private IHyBankService hyBankService;

    @Autowired
    private IHyCfgService hyCfgService;

    @Autowired
    private IHyCfgItemService hyCfgItemSerivce;

    @Value(value = "${mabang-contract.url}")
    private String url;

    @Value(value = "${bpt-2.0.url}")
    private String bpturl;

    @Value(value = "${paycenter.url}")
    private String _paycenterUrl;

    public void initBankType(){
        List<HyBank> bankList = hyBankService.list();
        BankTypeUtil.initBankType(bankList);
    }

    public void setPayCenterBankList(List<HyBank> bankList){
        try{
            String data = JSONObject.toJSONString(bankList);
            HttpTools.postJson(_paycenterUrl + "/base/configBank",  data);
            System.out.println("初始化银行成功参数成功");
        }catch (Exception e){
            System.out.println("初始化银行参数失败：" + e.getMessage());
        }
    }

    public void setPayCenterCfg(){
        try{
            HttpTools.get(_paycenterUrl + "/base/cfg");
            System.out.println("初始化配置参数成功");
        }catch (Exception e){
            System.out.println("初始化配置参数失败：" + e.getMessage());
        }
    }

    public void setPayCenterConfig(HyConfig hyConfig){
        try{
            String data = JSONObject.toJSONString(hyConfig);
            HttpTools.postJson(_paycenterUrl + "/base/config",  data);
            System.out.println("初始化支付中心参数成功");
        }catch (Exception e){
            System.out.println("初始化支付中心参数失败：" + e.getMessage());
        }
    }
    //初始化交通部平台
//    public  void Initbpt(HyConfig hyConfig){
//        if( !hyConfig.getWzHbIzUse()&&
//            !hyConfig.getWzTjIzUse()&&
//            !hyConfig.getWzAhIzUse()&&
//            !hyConfig.getWzJsIzUse()&&
//            !hyConfig.getWzSxIzUse()&&
//            !hyConfig.getWzSdIzUse()
//        ){
//            return;
//        }
//
//        Object hyConfigObj = JSONObject.toJSON(hyConfig);
//        String hyConfigStr = hyConfigObj.toString();
//        try {
//            String res = HttpUtils.sendPost(bpturl+"init", hyConfigStr);
//            ResBaseBean resBaseBean = JSONObject.parseObject(res,ResBaseBean.class);
//            if(resBaseBean.getData()!=null){
//                HyConfigUtils2 hyConfigUtils = JSONObject.parseObject(resBaseBean.getData().toString(), HyConfigUtils2.class);
//                HyConfigUtils.setInitReturnBean(hyConfigUtils.getInitReturnBean());
//                HyConfigUtils.setTjinitReturnBean(hyConfigUtils.getTjinitReturnBean());
//                HyConfigUtils.setAhinitReturnBean(hyConfigUtils.getAhinitReturnBean());
//                HyConfigUtils.setJsinitReturnBean(hyConfigUtils.getJsinitReturnBean());
//
//                HyConfigUtils.setSxinitReturnBean(hyConfigUtils.getSxinitReturnBean());
//                HyConfigUtils.setSdinitReturnBean(hyConfigUtils.getSdinitReturnBean());
//            }
//        } catch (Exception e) {
//            log.error("部平台初始化失败---"+e.getMessage(),e);
//        }
//    }

    @PostConstruct
    public void initConfig() throws Exception {
        HyCfgUtil.initService(hyCfgService , hyCfgItemSerivce );
        HyCfgUtil.init();
        this.setPayCenterCfg();

        HyConfig hyConfig = hyConfigService.getById("1");
        if (hyConfig == null) {
            throw new Exception("config 不能为空");
        }
        HyConfigUtils.setConfig(hyConfig);

        this.setPayCenterConfig(hyConfig);

        this.initBankType();

        //this.Initbpt(hyConfig);

        if (hyConfig.getYyzContractIzUse()) {
            //初始化易云章应用配置
            EcloudPublicKey.init(hyConfig.getYyzContractAppkey(), hyConfig.getYyzContractSecrept(), "1.0", url);
        }

    }


}
